import 'package:flutter/material.dart';
import './counter.dart';


class CounterPage extends StatefulWidget {
  const CounterPage({super.key});

  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  final Counter _counter = Counter();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('ListenableBuilder Example')),
      body: CounterBody(counterNotifier: _counter),
      floatingActionButton: FloatingActionButton(
        onPressed: _counter.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}

class CounterBody extends StatelessWidget {
  const CounterBody({super.key, required this.counterNotifier});
  final Counter counterNotifier;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Text('Current counter value:'),
          ListenableBuilder(
            listenable: counterNotifier, 
            builder: (BuildContext context, Widget? child) {
              return Text('${counterNotifier.count}'); 
            }
          )
        ],
      ),
    );
  }
}